package com.xxl.job.executor.service.jobhandler;

import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.executor.dao.DataMapper;
import com.xxl.job.executor.entity.AllData;
import com.xxl.job.executor.entity.Package;
import com.xxl.job.executor.entity.PackageHead;
import com.xxl.job.executor.entity.Record;
import com.xxl.job.executor.util.JaxbUtil;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;

@Service
public class XmlToEntity {

    @Autowired
    private DataMapper dataMapper;
    //PACKAGE
    private String BBH;
    private String DWDM;
    private String DWMC;
    private String JLS;
    private String SCSJ;
    private String SJBBH;
    //DATA-RECORD-attribute
    private Integer NO;
    private String SID;
    //DATA_RECORD-element
    private String GATJMJZZSLH;
    private String GMSFHM;
    private String XM;
    private String XBDM;
    private String CSRQ;
    private String SSXQDM;
    private String DZMC;
    private byte[] SDXP;
    private String ZWY_ZWTXSJ;
    private String ZWY_ZWTZSJ;
    private String ZWE_ZWTXSJ;
    private String ZWE_ZWTZSJ;
    private String QFJG_GAJGMC;
    private String GATJMJZZYXQQSRQ;
    private String GATJMJZZYXQJZRQ;
    private String QFCS;
    private String TXZHM;
    private String GATJMJZZSLYYDM;
    private String GATJMJZZZZLXDM;
    private String GATJMJZZLZFSDM;
    private String SJR_XM;
    private String SJR_LXDH;
    private String SJR_YZBM;
    private String SJR_TXDZ;

    //DateFormat f1 = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");
    //DateFormat f2 = new SimpleDateFormat("yyyy-MM-dd");

    private int countInsert;
    private int countUpdate;

    public static String transferLongTimeToDate(String dateString) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
        Date date = df.parse(dateString);
        String now = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);
        return now;
    }

    public static String transferShortTimeToDate(String dateString) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        Date date = df.parse(dateString);
        String now = new SimpleDateFormat("yyyy-MM-dd").format(date);
        return now;
    }

    @XxlJob("insertData")
    public void testXmlToEntity() {

        try {
            File fileDir = new File("D:\\testData2");
            File[] fileList = fileDir.listFiles();

            for (int i = 0; i < fileList.length; i++) {

                File file = fileList[i];
                InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
                BufferedReader reader = new BufferedReader(isr);

                //单线程使用StringBuilder接收
                StringBuilder builder = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    builder.append(line); // 将读到的内容添加到 builder 中
                    builder.append("\n"); // 添加换行符
                }
                IOUtils.closeQuietly(reader);
                String xml = builder.toString();
                //测试是否读取成功
                //System.out.println("第" + i + "个" + xml);
                Package p = (Package) JaxbUtil.convertXmlStrToObject(xml, Package.class);
                //System.out.println(p);

                //提取RECORD集合
                ArrayList<Record> record = p.getDATA().getRECORD();
                //System.out.println(record);
                //提取PACKAGEHEAD对象
                PackageHead ph = p.getPACKAGEHEAD();
                //System.out.println(ph);
                //PACKAGEHEAD字段
                BBH = ph.getBBH();
                DWDM = ph.getDWDM();
                DWMC = ph.getDWMC();
                JLS = ph.getJLS();
                SCSJ = transferLongTimeToDate(ph.getSCSJ());
                SJBBH = ph.getSJBBH();


                //遍历RECORD集合，将值赋给AllData对象
                for (Record r : record) {
                    //System.out.println(r);
                    //RECORD字段
                    NO = r.getNO();
                    SID = r.getSID();
                    GATJMJZZSLH = r.getGATJMJZZSLH();
                    GMSFHM = r.getGMSFHM();
                    XM = r.getXM();
                    XBDM = r.getXBDM();
                    CSRQ = transferShortTimeToDate(r.getCSRQ());
                    SSXQDM = r.getSSXQDM();
                    DZMC = r.getDZMC();
                    SDXP = Base64.getDecoder().decode(r.getSDXP());
                    ZWY_ZWTXSJ = r.getZWY_ZWTXSJ();
                    ZWY_ZWTZSJ = r.getZWY_ZWTZSJ();
                    ZWE_ZWTXSJ = r.getZWE_ZWTXSJ();
                    ZWE_ZWTZSJ = r.getZWE_ZWTZSJ();
                    QFJG_GAJGMC = r.getQFJG_GAJGMC();
                    GATJMJZZYXQQSRQ = r.getGATJMJZZYXQQSRQ();
                    GATJMJZZYXQJZRQ = r.getGATJMJZZYXQJZRQ();
                    QFCS = r.getQFCS();
                    TXZHM = r.getTXZHM();
                    GATJMJZZSLYYDM = r.getGATJMJZZSLYYDM();
                    GATJMJZZZZLXDM = r.getGATJMJZZZZLXDM();
                    GATJMJZZLZFSDM = r.getGATJMJZZLZFSDM();
                    SJR_XM = r.getSJR_XM();
                    SJR_LXDH = r.getSJR_LXDH();
                    SJR_YZBM = r.getSJR_YZBM();
                    SJR_TXDZ = r.getSJR_TXDZ();

                    AllData all = new AllData(BBH, DWDM, DWMC, JLS, SCSJ, SJBBH, NO, SID,
                            GATJMJZZSLH, GMSFHM, XM, XBDM, CSRQ, SSXQDM, DZMC, SDXP, ZWY_ZWTXSJ, ZWY_ZWTZSJ,
                            ZWE_ZWTXSJ, ZWE_ZWTZSJ, QFJG_GAJGMC, GATJMJZZYXQQSRQ, GATJMJZZYXQJZRQ, QFCS,
                            TXZHM, GATJMJZZSLYYDM, GATJMJZZZZLXDM, GATJMJZZLZFSDM, SJR_XM, SJR_LXDH,
                            SJR_YZBM, SJR_TXDZ);
                    //System.out.println(all);
                    if (dataMapper.selectGMSFHM(GMSFHM) == 0) {

                        int a = dataMapper.insertData(all);
                        if (a == 1) {
                            ++countInsert;
                        }
                    } else {
                        int b = dataMapper.updateRecord(all);
                        if (b == 1) {
                            ++countUpdate;
                        }
                    }
                }
            }
            System.out.println("插入" + countInsert + "条记录");
            System.out.println("更新" + countUpdate + "次记录");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            countUpdate=0;

        }

    }

   /* public static void main(String[] args) {
   //new 对象
        XmlToEntity xml = new XmlToEntity();
        try {
            xml.testXmlToEntity();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/
}
